const app = getApp();
const InfoApi = require('../../../api/information.js');
const commonUtil = require('../../../utils/common.js');

Page({
  data: {
    detailModel: {
      newsTitle: '', newsText: '',
      sourceName: '', publishTime: ''
    },
    fsUrl: app.globalData.fsUrl,
    optionsData: {
      newsTitle: null,
      newsId: null,
    }
  },

  onLoad(options) {
    console.log(options);

    if (options.newsTitle) {
      this.setData({
        ['optionsData.newsTitle']: options.newsTitle,
        ['optionsData.newsId']: options.newsId,
      })
    }
  },

  onReady() {

  },

  onShow() {
    wx.removeStorageSync('detailBack');

    if (this.data.optionsData.newsId) {
      wx.setNavigationBarTitle({ title: this.data.optionsData.newsTitle })
      this.getFrontNews(this.data.optionsData.newsId)
    } else {
      this.initData()

      let _newsInfo = wx.getStorageSync('newsInfo');
      console.log(_newsInfo);
      let _newsTitle = _newsInfo.newsTitle || _newsInfo.itemTitle;
      let itemLinkArr = [];
      let _linkNewsId = null;
      if (_newsInfo.itemLink) {
        itemLinkArr = _newsInfo.itemLink.split('/');
        _linkNewsId = _newsInfo.itemLink.indexOf('tongdow') > -1 ? itemLinkArr[itemLinkArr.length-1] : null;
      }
      let _newsId = _newsInfo.newsId ? _newsInfo.newsId : _linkNewsId;
      wx.setNavigationBarTitle({ title: _newsTitle })
      this.getFrontNews(_newsId)
    }
  },

  onHide() {
    this.initData()
  },
  onUnload() {
    wx.setStorageSync('detailBack', true);
    // wx.removeStorageSync('newsInfo');
    this.initData()
  },
  initData() {
    this.setData({
      detailModel: {
        newsTitle: '', newsText: '',
        sourceName: '', publishTime: ''
      },
      ['optionsData.newsTitle']: null,
      ['optionsData.newsId']: null,
    })
  },
  // 查询前端新闻详情
  getFrontNews(_newsId){
    InfoApi.getFrontNews({newsId: _newsId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        res.data.newsText = res.data.newsText.replaceAll('<p>　　</p>', '<br/>');
        res.data.newsText = res.data.newsText.replaceAll('<p>&nbsp;</p>', '<br/>');
        this.setData({
          detailModel: res.data
        })
      } else {
        if (res.errCode == '999901') {
          let _newsInfo = wx.getStorageSync('newsInfo');
          let _optionsData = this.data.optionsData;
          let _newsTitle = _optionsData.newsTitle ? _optionsData.newsTitle : _newsInfo ? _newsInfo.newsTitle : '';
          let _newsId = _optionsData.newsId ? _optionsData.newsId : _newsInfo ? _newsInfo.newsId : '';
          commonUtil.handleTokenExpire('/packageA/pages/newsDetail/index&newsTitle='+_newsTitle+'&newsId='+_newsId, 'link')
        }
      }
    })
  },

  onPullDownRefresh() {

  },

  onReachBottom() {

  },

  onShareAppMessage() {
    return {
      title: this.data.detailModel.newsTitle,
      path: "/packageA/pages/newsDetail/index?newsId="+this.data.detailModel.newsId+"&newsTitle="+this.data.detailModel.newsTitle,
    }
  },

  onShareTimeline() {
    return {
      title: this.data.detailModel.newsTitle,
      query: "newsId="+this.data.detailModel.newsId+"&newsTitle="+this.data.detailModel.newsTitle,
    }
  }
})